package com.yyh.synctool.client.test;

import com.yyh.common.IOCommon;
import com.yyh.synctool.client.tool.TaskMapper;
import com.yyh.synctool.client.tool.DBHelper;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

/**
 * @author xuan
 * @date 2021/12/14
 */
public class TaskDataSourceFactory
{

    /** 获取数据源 --不具有连接池 */
    public static DataSource getDataSource(String driverName, String url, String username, String password) {
        UnpooledDataSource unpooledDataSource = new UnpooledDataSource();
        unpooledDataSource.setDriver(driverName);
        unpooledDataSource.setUrl(url);
        unpooledDataSource.setUsername(username);
        unpooledDataSource.setPassword(password);
        unpooledDataSource.setAutoCommit(false);

        return unpooledDataSource;
    }

    public static SqlSessionFactory getSqlSessionFactory(DataSource dataSource) {
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("prod", transactionFactory, dataSource);
        Configuration configuration = new Configuration(environment);
        //configuration.addMapper(TaskMapper.class);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

        return sqlSessionFactory;
    }

    // 动态加载数据库配置和SQL语句配置，并执行
    @Test
    public void test() throws FileNotFoundException {
        String driverName = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://223.247.213.71:3306/HYEY";
        String username = "root";
        String password = "!qazHYEYLISEN";

        DataSource dataSource = getDataSource(driverName, url, username, password);
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(dataSource);
        Configuration configuration = sqlSessionFactory.getConfiguration();

        String xml = getXml();
        IOCommon.writeToString(new FileOutputStream("C:\\Users\\xuanjy\\Desktop\\TaskMapper.txt"), xml);
        System.out.println(xml);
        String namespace = "com.yyh.synctool.mapper.TaskMapper";
        ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes());

        XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(
                inputStream, configuration, namespace, configuration.getSqlFragments());
        xmlMapperBuilder.parse();
        System.out.println("加载完毕！");


        SqlSession sqlSession = sqlSessionFactory.openSession();
        TaskMapper mapper = sqlSession.getMapper(TaskMapper.class);
        List<Map> stock = mapper.findStock();
        System.out.println(stock);

        System.out.println(mapper);

    }

    public String getXml() {
        StringBuilder tmp = new StringBuilder();
        tmp.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        tmp.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\"\n");
        tmp.append("        \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">\n");
        tmp.append("<mapper namespace=\"com.yyh.synctool.client.tool.TaskMapper\">\n");
        tmp.append("    <select id=\"findStock\" resultType=\"Map\">\n");
        tmp.append("        select * from order_payment_info\n");
        tmp.append("    </select>\n");
        tmp.append("</mapper>");

        return tmp.toString();
    }

    @Test
    public void test2() {
        String driverName = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://223.247.213.71:3306/HYEY";
        String username = "root";
        String password = "!qazHYEYLISEN";
        String namespace = "com.yyh.synctool.client.tool.TaskMapper";
        String xml = getXml();

        DBHelper dbHelper = new DBHelper();

        // 初始化数据库配置
        dbHelper.init(driverName, url, username, password);

        // 注册TaskMapper
        dbHelper.registryMapper(namespace, xml);

        TaskMapper mapper = dbHelper.getMapper(TaskMapper.class);
        List<Map> stock = mapper.findStock();

        stock.forEach(e -> System.out.println(e));
    }
}
